package com.aimosheng.phonelive;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.aimosheng.phonelive.activity.LauncherActivity;
import com.alibaba.android.arouter.launcher.ARouter;
import com.meihu.beautylibrary.MHSDK;
import com.mob.MobSDK;
//import com.mob.adsdk.AdConfig;
//import com.mob.adsdk.AdSdk;
import com.mob.adsdk.AdConfig;
import com.mob.adsdk.AdSdk;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.rtmp.TXLiveBase;
import com.tencent.rtmp.TXLiveConstants;
import com.aimosheng.common.CommonAppConfig;
import com.aimosheng.common.CommonAppContext;
import com.aimosheng.common.utils.DecryptUtil;
import com.aimosheng.common.utils.L;
import com.aimosheng.im.utils.ImMessageUtil;

//import com.squareup.leakcanary.LeakCanary;

/**
 * Created by cxf on 2017/8/3.
 */

public class AppContext extends CommonAppContext {

    public static AppContext sInstance;
    private boolean mBeautyInited;

    private static final String APP_ID = "de1d4a4f6d11df2760"; // 正式APP_Id
//    private static final String APP_ID = "ba0063bfbc1a5ad878"; // 这是测试 appId，请替换成分配到的正式 appId
    private static final long MIN_SPLASH_INTERVAL = 3 * 60 * 1000L;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        //腾讯云鉴权url          update---20210507
        String liveLicenceUrl = "http://license.vod2.myqcloud.com/license/v1/e6f2c73c155a85162f22b2a181482236/TXLiveSDK.licence";
        //腾讯云鉴权key
        String liveLicenceKey = "97b469b8c2e47a71fd3766548c4a0880";
        TXLiveBase.getInstance().setLicence(this, liveLicenceUrl, liveLicenceKey);
        TXLiveBase.setConsoleEnabled(true);
        TXLiveBase.setLogLevel(TXLiveConstants.LOG_LEVEL_DEBUG);
        L.setDeBug(BuildConfig.DEBUG);
        //初始化腾讯bugly
        CrashReport.initCrashReport(this);
        CrashReport.setAppVersion(this, CommonAppConfig.getInstance().getVersion());
        //初始化ShareSdk
        MobSDK.init(this);
        //初始化极光推送
        //ImPushUtil.getInstance().init(this);
        //初始化IM
        ImMessageUtil.getInstance().init();
        //初始化西域AD - SDK
        initAdSdk();
        //初始化 ARouter
        if (BuildConfig.DEBUG) {
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(this);
//        if (!LeakCanary.isInAnalyzerProcess(this)) {
//            LeakCanary.install(this);
//        }
    }

    private void initAdSdk() {
        AdSdk.getInstance().init(getApplicationContext(),
                new AdConfig.Builder()
                        .appId(APP_ID)
                        // .userId(userId) // 未登录可不设置 userId，登录时再设置
                        .debug(BuildConfig.DEBUG)
                        .build(),
                null);
//        setMinSplashInterval(MIN_SPLASH_INTERVAL);
    }


    /** 设置最短开屏间隔时间，如果切到后台超出此间隔时间，回到前台任意页面都会显示开屏，以提升开屏广告的曝光量 */
    private void setMinSplashInterval(final long minSplashInterval) {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            private int activityCount = 0;
            private long leaveTime = 0;

            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                if (0 == activityCount++) {
                    // App 回到前台
                    if (System.currentTimeMillis() - leaveTime >= minSplashInterval) {
                        if (!(activity instanceof LauncherActivity)) {
                            Intent intent = new Intent(activity, LauncherActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); // 无动画
                            activity.startActivity(intent);
                        }
                    }
                }
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                if (0 == --activityCount) {
                    // App 切到后台
                    leaveTime = System.currentTimeMillis();
                }
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
            }
        });
    }

    /**
     * 初始化美狐
     */
    public void initBeautySdk(String beautyKey) {
        if (!TextUtils.isEmpty(beautyKey)) {
            if (!mBeautyInited) {
                mBeautyInited = true;
                if (CommonAppConfig.isYunBaoApp()) {
                    beautyKey = DecryptUtil.decrypt(beautyKey);
                }
                MHSDK.init(this, beautyKey);
                CommonAppConfig.getInstance().setMhBeautyEnable(true);
                L.e("美狐初始化------->" + beautyKey);
            }
        } else {
            CommonAppConfig.getInstance().setMhBeautyEnable(false);
        }
    }

}
